<?php

/**
 * @content: 接收上传过来的巡查记录数据
 * @Author:  Groube
 * @Date:    2017-03-09 17:30:00
 * @version  $Id
 */
class XunchadatauploadController extends Yaf_Controller_Abstract {

    /**
     * @Title  禁用模板功能
     * @Author Groube
     * @Param  /
     * @Return /
     * @Date 2017/03/08 14:30
     */
    public function init() {
        $yiyuan_id = isset($_REQUEST['yiyuan_id']) ? $_REQUEST['yiyuan_id'] : "";

        if($yiyuan_id === "" ){
            Utility::apiJsonError(20028, '请输入医院ID');
        }elseif(!ctype_digit("$yiyuan_id")){
            Utility::apiJsonError(20027, '医院ID必须是整型');
        }

        Yaf_Dispatcher::getInstance()->disableView();
    }


    /**
     * @Title  入口方法
     * @Author Groube
     * @Param  /
     * @Return /
     * @Date 2017/03/08 18:00
     */
    function indexAction(){
        $request = $this->getRequest();
        $data = $request->getPost('data','');
        $user_department_id = $request->getPost('user_department_id','');
        $str_yiyuan_id = $request->getPost('yiyuan_id',0);
        $yiyuan_id = (int)$str_yiyuan_id;

        if(Yaconf::get("config$yiyuan_id.fenbiao_state") == 'true'){
            //分表机制开启
            $zhuyuan_xuncha_jilu = "zhuyuan_xuncha_jilu_".$user_department_id;
            $zhuyuan_xuncha_jilu_detail = "zhuyuan_xuncha_jilu_detail_".$user_department_id;
        }else{
            $zhuyuan_xuncha_jilu = "zhuyuan_xuncha_jilu";
            $zhuyuan_xuncha_jilu_detail = "zhuyuan_xuncha_jilu_detail";
        }

        /*是否开启压缩 默认开启*/
        $compression_http_data = $request->getPost('compression_http_data','off');

        //解压缩数据
        $upload_data = json_decode(Utility::uncompress($data,$compression_http_data),true);

        if(empty($upload_data)){
            Utility::apiJsonError(40015,'(Method:接收上传过来的巡查记录数据)数据不能为空!');
        }

        $db = Kohana_Database::instance();
        $db->begin();
        $status = '1';

        try{
            foreach($upload_data as $one){
                $add_string = "";
                $xuncha_jilu_data = array(
                    'zhuyuan_id' => trim($one['zhuyuan_id']),
                    'xuncha_time' => $one['xuncha_time'],
                    'xuncha_hushi_id' => trim($one['xuncha_hushi_id']),
                    'xuncha_hushi_name' => trim($one['xuncha_hushi_name']),
                    'modify_time' => date('Y-m-d H:i:s',time()),
                    'yiyuan_id' => $yiyuan_id
                );

                list($insert_id,$affected_rows) = DB::insert($zhuyuan_xuncha_jilu)->columns(array_keys($xuncha_jilu_data))->values(array_values($xuncha_jilu_data))->execute();

                foreach($one['detail'] as $detail_one){
                    $detail_data[] = array(
                        'xuncha_jilu_id' => $insert_id,
                        'xuncha_item_code' => $detail_one['xuncha_item_code'],
                        'xuncha_item_name' => trim($detail_one['xuncha_item_name']),
                        'xuncha_item_type' => trim($detail_one['xuncha_item_type']),
                        'modify_time' => date('Y-m-d H:i:s',time()),
                        'yiyuan_id' => $yiyuan_id
                    );

                    if(Yaconf::get("config$yiyuan_id.xuncha_huixie_hulijilu") == 'true' && $detail_one['xuncha_item_type'] == "巡查事件"){
                        if(empty($add_string)){
                            $add_string .= trim($detail_one['xuncha_item_name']);
                        }else{
                            $add_string .= ",".trim($detail_one['xuncha_item_name']);
                        }
                    }
                }

                if($add_string){
                    $this->xunchaWriteBackToHuLi($yiyuan_id,trim($one['zhuyuan_id']), trim($one['xuncha_hushi_id']), trim($one['xuncha_hushi_name']), $add_string, $one['xuncha_time']);
                }
            }

            Utility::multiInsert($zhuyuan_xuncha_jilu_detail,$detail_data);
            $db->commit();
        }catch (Kohana_Exception $e){
            $db->rollback();
            $status = '0';
        }

        $statr = array('state' => $status);
        $statr['msg'] = "";
        $statr['error'] = "200";

        $async_result_json = json_encode($statr);

        //压缩数据
        $async_result_json = Utility::compress($async_result_json,$compression_http_data);
        echo $async_result_json;
    }


    /**
     * 巡查记录回写护理记录单
     * @param string $zhuyuan_id
     * @param string $hushi_id
     * @param string $hushi_name
     * @param string $string
     */
    public function xunchaWriteBackToHuLi($yiyuan_id,$zhuyuan_id = '', $hushi_id = '', $hushi_name = '', $string = '', $zhixing_time = ''){
        $sql = "SELECT * FROM data_hulijilu_template WHERE hulicol_name = 'bqgc_hlcs' AND xiangmu_name = '病情观察与护理' AND (keshi = '通用' OR keshi IN (SELECT zhuyuan_bingqu FROM zhuyuan_basic_info WHERE zhuyuan_id = :zhuyuan_id AND yiyuan_id = :yiyuan_id)) AND yiyuan_id = :yiyuan_id";
        if(Yaconf::get("config$yiyuan_id.hospital_name") == '大理白求恩外科医院'){
            $hulijibie = DB::select('hulijibie')->from('zhuyuan_basic_info')->where('yiyuan_id','=',$yiyuan_id)->where('zhuyuan_id','=',$zhuyuan_id)->execute()->as_array();
            if($hulijibie == '一级护理'){
                $sql .= " AND biaodan_name like '危重%'";
            }else{
                $sql .= " AND biaodan_name like '一般%'";
            }
        }

        $query = DB::query(Database::SELECT, $sql);
        $query->param(':zhuyuan_id',$zhuyuan_id);
        $query->param(':yiyuan_id',$yiyuan_id);
        $temps = $query->execute()->as_array();

        if($temps){
            foreach ($temps as $k => $hulijilu){
                $huli_jilu_data[$k]['id'] = date('YmdHis') . str_pad(mt_rand(0, 9999), 4, '0', STR_PAD_LEFT);
                $huli_jilu_data[$k]['zhuyuan_id'] = $zhuyuan_id;
                $huli_jilu_data[$k]['type'] = $hulijilu['biaodan_name'];
                $huli_jilu_data[$k]['jilu_hushi_name'] = $hushi_name;
                $huli_jilu_data[$k]['jilu_hushi_id'] = $hushi_id;
                $huli_jilu_data[$k]['jilu_time'] = !empty($zhixing_time)?$zhixing_time:date('Y-m-d H:i:s');
                $huli_jilu_data[$k]['bqgc_hlcs'] = $string;
            }

            Utility::multiInsert('zhuyuan_huli_wenshu_detail',$huli_jilu_data);
        }
    }
}